$(function(){
	// 处理库存操作的弹出层
	$("a[name='numberInventoryBtn']").click(function(){
		var wareId = $(this).attr("dataId");
		showLayout("numberInventoryLi", wareId, "ni", "修改【"+getWareNameById(wareId)+"】库存");
	});
	// 处理价格操作的弹出层
	$("a[name='priceBtn']").click(function(){
		var wareId = $(this).attr("dataId");
		showLayout("priceLi", wareId, "p", "修改【"+getWareNameById(wareId)+"】价格");
	});
	// 处理上架操作的弹出层
	$("a[name='onShelfBtn']").click(onShelf);
	// 处理下架操作的弹出层
	$("a[name='offShelfBtn']").click(offShelf);
	// 处理删除操作的弹出层
	$("a[name='deleteBtn']").click(function(){
		var wareId = $(this).attr("dataId");
		showLayout("confirmLi", wareId, "del", "删除商品", "/images/delete_72px.png", "确定要删除【"+getWareNameById(wareId)+"】？删除后将不可恢复！");
	});
	// 点击弹出层确定按钮事件
	$("#submitBtn").click(submitOp);
	// 弹出层退出
	$(".tiptop a, .cancel, #closeBtn").click(function() {
		$("#tipDiv").fadeOut(200);
	});
});
/**
 * 点击弹出层的提交按钮之后的操作：
 * 1、根据不同的操作类型AJAX提交后台处理；
 * 2、JS修改列表页显示数据
 */
function submitOp() {
	var wareId = $("#wareId").val();
	var url = "";
	var data = {};
	switch($("#opType").val()) {
		case "ni": // 编辑库存
			url = "ni/"+wareId+".html";
			var number = $("#numberInventory").val();
			if(number) {
				data.number = number;
			} else {
				// TODO 验证数字
				alert("请填写库存数量");
				return false;
			}
			break;
		case "p": // 编辑价格
			url = "price/"+wareId+".html";
			var pricePromotional = $("#pricePromotional").val();
			var priceOriginal = $("#priceOriginal").val();
			var score = $("#score").val();
			if(pricePromotional) {
				pricePromotional && (data.pricePromotional = pricePromotional);
				priceOriginal && (data.priceOriginal = priceOriginal);
				score && (data.score = score);
			} else {
				alert("请填写优惠价");
				return false;
			}
			break;
		case "on": // 上架
			url = "onShelf/"+wareId+".html";
			break;
		case "off": // 下架
			url = "offShelf/"+wareId+".html";
			break;
		case "del": // 删除
			url = "del/"+wareId+".html";
			break;
	}
	/**
	 * POST提交请求
	 */
	$.post("/ware/info/" + url, data, function(d) {
		if(d && d == "1") {
			// 刷新数据
			switch($("#opType").val()) {
				case "ni": // 设置库存
					$("#ware_number_" + wareId).html(data.number);
					break;
				case "p": // 编辑价格
					$("#ware_pricePromotional_" + wareId).html(data.pricePromotional);
					$("#ware_priceOriginal_" + wareId).html(data.priceOriginal);
					$("#ware_score_" + wareId).html(data.score);
					break;
				case "on": // 上架
					$("#ware_status_" + wareId).html("在售");
					// 修改下架按钮为上架
					$("a[dataId='"+wareId+"'][name='onShelfBtn']").html("下架").attr("name", "offShelfBtn").unbind("click").click(offShelf);
					// 移除删除按钮 TODO 在售不允许删除
					break;
				case "off": // 下架
					$("#ware_status_" + wareId).html("下架");
					// 修改下架按钮为上架
					$("a[dataId='"+wareId+"'][name='offShelfBtn']").html("上架").attr("name", "onShelfBtn").unbind("click").click(onShelf);
					// 增加删除按钮 TODO
					
					break;
				case "del": // 删除
					$("#ware_tr_" + wareId).remove();
					break;
			}
			showLayout("confirmLi", null, "", "操作结果", "/images/success_72px.png", "操作成功！");
//			$("#tipDiv").fadeOut(200);
		}
	}, "text");
}
/**
 * 点击下架按钮后调用方法
 */
function offShelf() {
	var wareId = $(this).attr("dataId");
	showLayout("confirmLi", wareId, "off", "下架商品", "/images/warning_72px.png", "确定要下架【"+getWareNameById(wareId)+"】？下架后用户将不能购买此商品！");
}
/**
 * 点击上架按钮后调用方法
 */
function onShelf() {
	var wareId = $(this).attr("dataId");
	showLayout("confirmLi", wareId, "on", "上架商品", "/images/warning_72px.png", "确定要上架【"+getWareNameById(wareId)+"】？");
}

/**
 * 显示弹出层
 */
function showLayout(showContentEl, wareId, opType, title, imgUrl, text) {
	$(".contentWapper").hide();
	$("#opType").val(opType); // 设置操作类型：ni是number inventory简写
	if(imgUrl) {
		$(".tip-confim-img").attr("src", imgUrl);
		$(".tip-confim-text").html(text);
	}
	$("#tipTitie").html(title);
	if(wareId) {
		$("#wareId").val(wareId);
		$("#submitBtn").show();
	} else {
		$("#submitBtn").hide();
	}
	$("li[name='"+showContentEl+"']").show();
	$("#tipDiv").fadeIn(200);
}

/**
 * 根据商品ID从表格获取商品名称
 */
function getWareNameById(id) {
	return $("#ware_name_" + id).html();
}
